<!-- 授权登录页 -->
<template>
	<view class="page">
		<view class="_img">
			<image src="https://pigpig22021.oss-cn-hangzhou.aliyuncs.com/imgs/home/c-logintop.png" mode="widthFix">
			</image>
		</view>
		<view class="title">
			<image src="https://pigpig22021.oss-cn-hangzhou.aliyuncs.com/imgs/home/c-logintext.png" mode="widthFix">
			</image>
		</view>
		<view v-show="stauts == 0" class="authorization">
			<button type="default" @click="authorization">
				<view class="botton">
					开启登录授权
				</view>
			</button>
		</view>
		<view v-show="stauts == 1" class="authorization">
			<button type="default" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">
				<view class="botton">
					开启手机号授权
				</view>
			</button>
		</view>
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userinfo: {

				},
				stauts: 0,
				time: 0,
				date: 0
			}
		},
		methods: {
			//登录授权
			authorization() {
				if(this.time == 1){
					return
				}
				this.time = 1
				uni.getUserProfile({
					desc: '登录',
					success: (res) => {
						console.log(res)
						this.userinfo = res.userInfo
						this.userinfo.code = uni.getStorageSync('code')
						this.getList()
						this.$refs.uTips.show({
							title: '授权成功,请继续授权手机号!',
							type: 'success',
							duration: '2300'
						})
					},
					fail: (e) => {
						return e
					}
				})
			},
			//手机号授权
			async decryptPhoneNumber(e) {
				if(this.date == 1){
					return
				}
				this.date = 1
				let sessionKey = uni.getStorageSync('sessionKey')
				let openId = uni.getStorageSync('openId')
				let arr = {}
				arr.iv = e.detail.iv
				arr.encryptedData = e.detail.encryptedData
				arr.sessionKey = sessionKey
				arr.openId = openId
				let {
					data
				} = await this.$post('/phone', arr, '')
				uni.navigateBack({
					delta: 1
				})
			},
			//授权接口
			
			async getList() {
				let {
					data
				} = await this.$post('/backend/v1/vxLogin', this.userinfo, '')
				console.log(data)
				uni.setStorageSync("openId", data.openId)
				uni.setStorageSync("sessionKey", data.sessionKey)
			}
		}
	}
</script>

<style lang="scss">
	.page {
		width: 100%;
		height: 100%;

		._img {
			border-radius: 50%;
			width: 240rpx;
			height: 240rpx;
			background-color: #d5f6ff;
			overflow: hidden;
			margin: 0 auto;
			margin-top: 146rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.title {
			width: 298rpx;
			height: 36rpx;
			margin: 39rpx auto 0;

			image {
				width: 100%;
			}
		}

		.authorization {
			button {
				margin-top: 119rpx;
				width: 560rpx;
				height: 90rpx;
				line-height: 90rpx;
				background-image: linear-gradient(-90deg,
						#fc554c 0%,
						#fd7057 100%),
					linear-gradient(#5295df,
						#5295df);
				background-blend-mode: normal,
					normal;
				border-radius: 45rpx;

				.botton {

					letter-spacing: 2rpx;
					color: #ffffff;
				}
			}
		}
	}
</style>
